package wechat.handler.response.business.message.receive;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import wechat.handler.bean.response.MessageBaseBean;
import wechat.handler.bean.response.message.receive.ReceiveTextBean;
import wechat.handler.bean.response.message.reply.ReplyTextBean;
import wechat.handler.resources.AppFilter;
import wechat.handler.resources.InstanceResource;
import wechat.handler.util.Configure;

public class ReceiveTextBusiness {
	private Logger log = LoggerFactory.getLogger(ReceiveTextBusiness.class);
	
	private ReceiveTextBean receiveTextBean;
	private InstanceResource instanceResource;
	
	public ReceiveTextBusiness(ReceiveTextBean receiveTextBean){
		this.receiveTextBean = receiveTextBean;
	}
	
	public ReceiveTextBusiness(String appid,ReceiveTextBean receiveTextBean){
		if (appid != null) {
			instanceResource = AppFilter.chooseApp(appid);
		}
		this.receiveTextBean = receiveTextBean;
	}
	
	public ReceiveTextBusiness(String str) throws Exception{
		this.receiveTextBean = new ReceiveTextBean(str);
		this.receiveTextBean.transform();
	}
	
	public ReceiveTextBusiness(String appid,String str) throws Exception{
		if (appid != null) {
			instanceResource = AppFilter.chooseApp(appid);
		}
		this.receiveTextBean = new ReceiveTextBean(str);
		this.receiveTextBean.transform();
	}
	
	public String getReplyText() throws Exception{
		String openId = receiveTextBean.getFromUserName();
		
		ReplyTextBean replyTextBean = new ReplyTextBean();
		if (instanceResource != null) {
			replyTextBean.setFromUserName(instanceResource.getWechatid());	
			replyTextBean.setContent(instanceResource.getCommonContent());
		}
		else {
			replyTextBean.setFromUserName(Configure.getWechatid());
			replyTextBean.setContent("");
		}
		
		replyTextBean.setToUserName(openId);
		replyTextBean.setCreateTime(System.currentTimeMillis());
		replyTextBean.setMsgType(MessageBaseBean.Reply_Message_Text);
	
		
		return replyTextBean.buildXml();
	}

	public InstanceResource getInstanceResource() {
		return instanceResource;
	}
}
